
//
//  RecordListViewController.swift
//  IDF
//
//  Created by 陈徐挺 on 22/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit

class RecordListViewController: BaseViewController {

    @IBOutlet weak var baseTableView: UITableView!
    @IBOutlet weak var noDataView: UIView!

    var cellHeight: [CGFloat]!

    let data = [
        "Auditing",
        "Through audit",
        "Loan success",
        "Audit failure",
        "Repaid",
        "Repaid audit",
        "Overdue",
    ]
    var recordData: [Record] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Record"
        baseTableView.separatorStyle = .none
        baseTableView.register(cellType: RecordTableViewCell.self)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if appLaunched {
            if !appLogined {
                showLoginView()
                mainTabBarController.selectedIndex = 0
                nowNavgationController = mainTabBarController.viewControllers![0] as! UINavigationController
            } else {
                getData()
            }
        } else {
            let payBackStepView = PayBackStepView(frame: appWindow.bounds)
            appWindow.addSubview(payBackStepView)
            payBackStepView.okBtn.setBlockFor(.touchUpInside) { [weak self] (btn) in
                payBackStepView.removeFromSuperview()
                if appLogined {
                    self?.getData()
                } else {
                    appLaunched = true
                    mainTabBarController.selectedIndex = 0
                    nowNavgationController = mainTabBarController.viewControllers![0] as! UINavigationController
                }
            }
        }
    }

    func getData() {
        let _ = Record.getList().then { [weak self] res -> Void in
            if res.count == 0 {
                self?.noDataView.isHidden = false
                self?.baseTableView.isHidden = true
                if !appLaunched {
                    appLaunched = true
                    mainTabBarController.selectedIndex = 0
                    nowNavgationController = mainTabBarController.viewControllers![0] as! UINavigationController
                }
            } else {
                appLaunched = true
                getUserDebitAttention()
                self?.noDataView.isHidden = true
                self?.baseTableView.isHidden = false
                self?.recordData = res
                self?.cellHeight = Array(repeating: 0, count: res.count)
                self?.baseTableView.reloadData()
            }

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension RecordListViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return recordData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RecordTableViewCell = tableView.dequeueReusableCell(for: indexPath)
        cell.setCell(recordData[indexPath.row])
        if cellHeight.count > indexPath.row {
            cellHeight[indexPath.row] = cell.getCellHeight()
        }

        return cell
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)

        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return cellHeight[indexPath.row]
        }
    }
